//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "mainfrm.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    postcodeLookup = CoSimplyPostCode::Create();
    
    //Assign your data key to identify your account
    theKey = SysAllocString(L"Your Data Key");
    postcodeLookup->SetDataKey( &theKey );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    SysFreeString(theKey);
}
//---------------------------------------------------------------------------

// #### The function below implements search using our listbox to provide searching for postcode address

void __fastcall TForm1::btnLookupDialogueClick(TObject *Sender)
{
    //Use Simply Postcode search dialog box
    BSTR  thePostCode = SysAllocString(L"");
    BSTR  theCaption = SysAllocString(L"Get Address");
    TOLEBOOL val1 = true;

    lstAddresses->Clear();
    if( postcodeLookup->SearchForFullAddressWithDialogue(&thePostCode, &theCaption,  &val1, &val1, &val1) )
    {
        FillAddress();
    }

    SysFreeString(thePostCode);
    SysFreeString(theCaption);
}
//---------------------------------------------------------------------------


// #### The function below implements search using our listbox to provide searching for postcode address
// #### and advanced searching, which requires a Folder to store data in (used for selection lists aprox 2kb)

void __fastcall TForm1::btnLookupDialogueClickAdv(TObject *Sender)
{
    //Use Simply Postcode search dialog box
    BSTR  thePostCode = SysAllocString(L"");
    BSTR  theCaption = SysAllocString(L"Get Address");
    BSTR  theDataStore = SysAllocString(L"C:\");
    TOLEBOOL val1 = true;

    lstAddresses->Clear();
    if( postcodeLookup->SearchForFullAddressWithDialogueAdv(&thePostCode, &theCaption, &theDataStore, &val1, &val1, &val1) )
    {
        FillAddress();
    }

    SysFreeString(thePostCode);
    SysFreeString(theCaption);
}
//---------------------------------------------------------------------------






// #### All code below implements search using your own listbox to list address 


void __fastcall TForm1::btnFindClick(TObject *Sender)
{
    //Find button
    AnsiString AddressLine;

    BSTR MyBstr = AnsiToBSTR(txtFindPostCode->Text.c_str());

    //Search for postcode
    if( postcodeLookup->GetFullAddressToList(&MyBstr) )
    {
    	//Clear list
        lstAddresses->Clear();
        
        //Get first entry to add to list
        AddressLine = postcodeLookup->GetFullAddressLineForSelection();
        while( AddressLine > "" )
        {
            //Add item to list box
            lstAddresses->Items->Add(AddressLine);
            AddressLine = postcodeLookup->GetFullAddressLineForSelection();
        }
    }
    else
    {
        //Report error message
        AnsiString errormsg = postcodeLookup->General_errormessage;
        Application->MessageBoxA(errormsg.c_str(),"", MB_OK);
    }

    SysFreeString(MyBstr);
}
//---------------------------------------------------------------------------



void __fastcall TForm1::lstAddressesClick(TObject *Sender)
{
    long AddressID = lstAddresses->ItemIndex;
    postcodeLookup->GetFullAddressRecord(&AddressID);

    FillAddress();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FillAddress()
{
    txtOrg->Text =  postcodeLookup->Address_Organisation;
    txtLine1->Text =  postcodeLookup->Address_Line1;
    txtTown->Text =  postcodeLookup->Address_Town;
    txtCounty->Text =  postcodeLookup->Address_County;
    txtPostcode->Text =  postcodeLookup->Address_Postcode;
}
//---------------------------------------------------------------------------

BSTR TForm1::AnsiToBSTR(const char* szAnsiString)
{
     if(szAnsiString)
     {
          int nAnsiStringLen = lstrlenA(szAnsiString);
          if(nAnsiStringLen)
          {
               wchar_t* UnicodeString = new wchar_t[nAnsiStringLen];
               MultiByteToWideChar(CP_ACP, 0, szAnsiString, nAnsiStringLen, UnicodeString, nAnsiStringLen);
               BSTR bstrRet = SysAllocStringLen(UnicodeString, nAnsiStringLen);
               delete[] UnicodeString;
               return bstrRet;
          }
          return SysAllocString(L""); // an empty ansi string passed - an empty BSTR returned
     }
     return NULL; // NULL passed - NULL returned
}

//---------------------------------------------------------------------------

